﻿using FPSO.Models;
using FPSO.Models.Constant;
using FPSO.Models.Domain;
using NSubstitute;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
using Ubiety.Dns.Core;
using System.Data;
using ExcelDataReader;

namespace FPSO.Services
{
	public class TaskUtils
	{
		public static DateTime CalcTaskEndTime(TankTask task)
		{
			return task.ExecStartTime.AddHours(task.TargetVolm * 0.01 * TankConstant.GetTankVolume(task.TankId) / task.PipeSpeed);
		}

		/// <summary>

		/// 读取txt文件，并返回文件中的内容

		/// </summary>

		/// <returns>txt文件内容</returns>

		public static string ReadTxTContent(string path)
		{
			try
			{
				string s_con = string.Empty;
				// 创建一个 StreamReader 的实例来读取文件 
				// using 语句也能关闭 StreamReader
				using (StreamReader sr = new StreamReader(path))
				{
					string line;
					// 从文件读取并显示行，直到文件的末尾 
					while ((line = sr.ReadLine()) != null)
					{
						s_con += line;
					}
				}
				return s_con;
			}
			catch (Exception e)
			{
				//Console.WriteLine(e.Message);
				return null;
			}
		}

		/// <summary>
		/// 异步读取txt文件内容
		/// </summary>
		/// <param name="filePath"></param>
		/// <returns></returns>
        public static async Task<string> ReadTxTContentAsync(string filePath)
        {
            try
            {
                using (StreamReader reader = new StreamReader(filePath))
                {
                    return await reader.ReadToEndAsync();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"异步读取文件失败 {filePath}: {ex.Message}");
                return string.Empty;
            }
        }

        public static List<string[]> ReadExcel(string strFileName)
		{
			FileStream fs = new FileStream(strFileName, FileMode.Open, FileAccess.Read, FileShare.None);
			StreamReader sr = new StreamReader(fs, System.Text.Encoding.GetEncoding(936));

			string str = "";
			string s = Console.ReadLine();
			List<string[]> rtn = new List<string[]>();
			while (str != null)
			{
				str = sr.ReadLine();
				if (str == null)
					continue;
				string[] xu = new String[2];
				xu = str.Split(',');
				string ser = xu[0];
				if (ser != s && xu.Length > 0 && xu[4].Length > 0) 
				{
					rtn.Add(xu);
				}
			}
			sr.Close();
			return rtn;
		}

		public static DataSet ReadExcelToDataSet(string fileNmaePath)
		{
			FileStream stream = null;
			IExcelDataReader excelReader = null;
			try
			{
				//stream = File.Open(fileNmaePath, FileMode.Open, FileAccess.Read);
				stream = new FileStream(fileNmaePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
			}
			catch
			{
				return null;
			}
			string extension = Path.GetExtension(fileNmaePath);
			if (extension.ToUpper() == ".XLS")
			{
				excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
			}
			else if (extension.ToUpper() == ".XLSX")
			{
				excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
			}
			else
			{
				return null;
			}
			DataSet dataSet = excelReader.AsDataSet(new ExcelDataSetConfiguration()
			{
				ConfigureDataTable = (_) => new ExcelDataTableConfiguration()
				{
					UseHeaderRow = true
				}
			});
			excelReader.Close();
			return dataSet;
		}
	}
}
